#!/bin/bash
#功能：删除默认网关
#命令：route del default gw 192.168.61.2
#SH
SHPATH=`readlink -f $0`
SHDIR=`dirname $SHPATH`
SHNAME="$0"

#DIR
LOG_DIR=$SHDIR/logs/$SHNAME
BAK_DIR=$SHDIR/bak/$SHNAME
TMP_DIR=$SHDIR/temp/$SHNAME
mkdir -p $LOG_DIR $BAK_DIR $TMP_DIR


#FILE
TMP_PATH=$TMP_DIR/route.list.temp
LOG_PATH=$LOG_DIR/`date +%F_%H%M%S`.route.log
BAK_PATH=$BAK_DIR/`date +%F_%H%M%S`.route.bak
BAK_INIT=$BAK_DIR/init_route.bak
#第一次备份初始路由
if [ ! -f $BAK_INIT ];then 
route -n > $BAK_INIT
echo "`date +%F_%H%M%S` INIT_BAK" >$LOG_DIR/init_bak.log
cat $BAK_INIT >>$LOG_DIR/init_bak.log
fi




#打印当前路由--------
echo "-------------------------------- 当前路由信息 --------------------------------" |tee -a $LOG_PATH
route -n 																			|tee -a $LOG_PATH
echo "----------------------------------------------------------------------------" |tee -a $LOG_PATH
if [ $# -eq 0 ];then
echo '
1.删除默认网关
2.恢复默认网关
0.退出
'
	read -t 10 -p "请10秒内输入(默认1):" KEY
	if [ -z $KEY ];then KEY=1;fi
elif [ $# -eq 1 ];then
	KEY=$1
else
	echo "Error,参数错误！参数仅可以1或2。"
fi


#判断输入的值
case $KEY in
	1)
	#获取网关
	GW_IP=`route -n |awk '{print $1,$2,$3,$4}'|grep UG$|awk '{print $2}'`

	#判断是否存在默认网关
	if [ -z "$GW_IP" ];then 
		echo "当前不存在默认网关信息，退出。"|tee -a $LOG_PATH
		exit
	else
		#备份路由
		route -n > $BAK_PATH
		#删除默认路由
		for i in `echo $GW_IP` 
		do
		echo "执行删除默认网关:$i" |tee -a $LOG_PATH
		route del default gw $i
		done
	fi

	#打印当前路由
	echo "-------------------------------- 路由修改完成 --------------------------------" |tee -a $LOG_PATH
	route -n 																			|tee -a $LOG_PATH
	echo "----------------------------------------------------------------------------" |tee -a $LOG_PATH
	echo "操作内容: "	`cat $LOG_PATH|grep '删除默认网关'|xargs -n1 `
	echo "操作内容: "	`cat $LOG_PATH|grep '删除默认网关'|xargs -n1 `							>>		$LOG_PATH												|tee -a $LOG_PATH
	echo "Out_log:	$LOG_PATH"
	echo "bak_dir:	$BAK_DIR"
	;;

	2)
	find $LOG_DIR -maxdepth 1 -type f  -name "*.log"|xargs grep '操作内容' |tail -5 > $TMP_PATH
	WC=`cat $TMP_PATH|wc -l`
	if [ $WC -eq 0 ];then echo "不存在恢复文件,退出！";exit;fi
	while true
	do
	echo "-------------------------------- 恢复操作 --------------------------------"
	cat -n $TMP_PATH
	echo "     0  退出"
	read -p "请输入序列(0-5):" NUM
	if [ $NUM -eq 0   ];then echo "退出。";exit ;fi
	if [ $NUM -gt $WC ];then echo "Error,输入错误，请重新输入！";continue ;fi

	LINE_VAR=`sed -n "${NUM}p" $TMP_PATH`
	LOG_FILE=`echo $LINE_VAR|awk -F ':操作内容:' '{print $1}' `
	LOG_EXEC=`echo $LINE_VAR|awk -F ':操作内容:' '{print $2}' `
		for i in `echo $LOG_EXEC`
		do
			GW_IP=`echo $i|awk -F ':' '{print $2}'`
			echo $i |grep "删除" >/dev/null
			if [ $? -eq 0 ];then
				echo "${LOG_FILE}执行恢复操作,添加默认网关:${GW_IP}" |tee -a $LOG_PATH
				route add default gw $GW_IP 
			else
				echo "${LOG_FILE}执行恢复操作,删除默认网关:${GW_IP}" |tee -a $LOG_PATH
				route del default gw $GW_IP
			fi
		done

	echo "-------------------------------- 路由恢复完成 --------------------------------" 	|tee -a $LOG_PATH
	route -n 																			|tee -a $LOG_PATH
	echo "----------------------------------------------------------------------------" |tee -a $LOG_PATH
	echo "操作内容: "	`cat $LOG_PATH|grep '恢复操作'|xargs -n1 `
	echo "操作内容: "	`cat $LOG_PATH|grep '恢复操作'|xargs -n1 `							>>		$LOG_PATH												|tee -a $LOG_PATH
	echo "Out_log:	$LOG_PATH"
	echo "bak_dir:	$BAK_DIR"

	#退出
	break
	done

	;;


	0)

	;;

	*)
	echo "输入错误退出"
	;;
esac




